package org.ruiyichen.guizhou.agr.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.base.BaseFragment;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.fragment.BaseFragmentPageAdapter;
import org.ruiyichen.guizhou.agr.ui.fragment.ControlFragment;
import org.ruiyichen.guizhou.agr.ui.fragment.HomeAgileFragment;
import org.ruiyichen.guizhou.agr.ui.fragment.HomeFragment;
import org.ruiyichen.guizhou.agr.ui.fragment.MineFragment;
import org.ruiyichen.guizhou.agr.ui.fragment.MonitorAgileFragment;
import org.ruiyichen.guizhou.agr.ui.fragment.MonitorFragment;
import org.ruiyichen.guizhou.agr.ui.mineview.NumberHProgressDialog;

import androidx.viewpager.widget.ViewPager;

/**
 * 农户数据采集页
 * Created by Administrator on 2018/8/12.
 */

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    private NumberHProgressDialog progressDialog;

    //人像
    private static final int REQUEST_CODE_CAMERA_TX = 101;
    //身份证正反面
    private static final int REQUEST_CODE_CAMERA_ZFM = 102;

    public static final int REQUEST_CODE_ADD_LAND = 103;
    public static final int REQUEST_CODE_SELECT_LAND = 104;

    public TextView[] mBtns;
    private ViewPager vp_fragment;
    //fragment的集合，对应每个子页面
    private BaseFragment[] fragments;
    //按键个数
    private final int BTN_SIZE = Constant.isAgile ? 2 : 4;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_home);
        bOkHttpManager = new OkHttpManager(this, bHandler);
    }

    @Override
    public void initViews() {

        progressDialog = NumberHProgressDialog.createDialog(this);

        mBtns = new TextView[BTN_SIZE];
        if (Constant.isAgile) {
            //待审核
            mBtns[0] = findViewById(R.id.tv_btn1);
            mBtns[0].setSelected(true);
            //审核成功
            mBtns[1] = findViewById(R.id.tv_btn3);

            findViewById(R.id.tv_btn2).setVisibility(View.GONE);
            findViewById(R.id.tv_btn4).setVisibility(View.GONE);
        } else {
            //待审核
            mBtns[0] = findViewById(R.id.tv_btn1);
            mBtns[0].setSelected(true);
            //审核成功
            mBtns[1] = findViewById(R.id.tv_btn2);

            //审核失败
            mBtns[2] = findViewById(R.id.tv_btn3);
            //已放款
            mBtns[3] = findViewById(R.id.tv_btn4);
        }


        //viewPage
        vp_fragment = findViewById(R.id.vp_fragment);

        //初始化界面
        fragments = new BaseFragment[BTN_SIZE];
        if (Constant.isAgile) {
            fragments[0] = new HomeAgileFragment();
            fragments[1] = new MonitorAgileFragment();
        } else {
            fragments[0] = new HomeFragment();
            fragments[1] = new ControlFragment();
            fragments[2] = new MonitorFragment();
            fragments[3] = new MineFragment();
        }


        vp_fragment.setAdapter(new BaseFragmentPageAdapter(getSupportFragmentManager(), fragments));

        vp_fragment.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectPage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initEvents() {
        for (int i = 0; i < mBtns.length; i++) {
            mBtns[i].setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_btn1:
                selectPage(0);
                break;
            case R.id.tv_btn2:
                selectPage(1);
                break;
            case R.id.tv_btn3:
                if (Constant.isAgile) {
                    selectPage(1);
                } else {
                    selectPage(2);
                }
                break;
            case R.id.tv_btn4:
                selectPage(3);
                break;
            case R.id.iv_normal_back:
                onBackPressed();
                break;
        }
    }

    private int curr_page;

    private void selectPage(int page) {
        if (page == curr_page) {
            return;
        }
        //底部标签选择状态
        for (int i = 0; i < mBtns.length; i++) {
            mBtns[i].setSelected(i == page);
        }
        //选择viewpage
        vp_fragment.setCurrentItem(page);
        curr_page = page;
        fragments[page].onHiddenChanged(false);
    }

}
